#include<iostream>
#include<queue>
using namespace std;

typedef struct GNode{
    int n;//图的顶点
    int e;//图的边数
    int juzhen[11][11];//邻接矩阵
};

struct GNode graph;//图的节点

//深度优先遍历
void DFS(int visited[],int start)
{
    visited[start] = 1;//定义访问过的节点为1
    cout<<start<<" ";//输出节点
    for(int i = 0;i<graph.n;i++)//遍历图的顶点数
        if(i!=start&&!visited[i]&&graph.juzhen[start][i])//对于没访问过的节点
            DFS(visited,i);//递归调用dfs
}

//广度优先遍历
void BFS(int visited[],int start)
{
    queue<int> q;//创建空队列
    q.push(start);
    visited[start] = 1;//访问过的定义为1
    while(!q.empty())//队列不为空
    {
        int top = q.front();
        cout<<top<<" ";
        q.pop();
        for(int i = 0;i<graph.n;i++)
            if(i!=top&&!visited[i]&&graph.juzhen[top][i])
            {
                q.push(i);
                visited[i] = 1;
            }
    }
}

int main()
{
    int n,e;
    cin>>n>>e;
    graph.e = e;
    graph.n = n;
    for(int i = 0;i<n;i++)
        for(int j = 0;j<n;j++)
            graph.juzhen[i][j] = 0;  //全初始化为0
    for(int i = 0;i<e;i++)
    {
        int a,b;
        cin>>a>>b;
        graph.juzhen[a][b] = graph.juzhen[b][a] = 1;   //相连通的点之间的邻接矩阵为1
    }
    int visited[11] = {0};
    for(int i = 0;i<n;i++)
        if(!visited[i])
        {
            cout<<"{ ";
            DFS(visited,i);
            cout<<"}"<<endl;
        }
    for(int i = 0;i<=10;i++)
    {
        visited[i] = 0;
    }
    for(int i = 0;i<n;i++)
        if(!visited[i])
        {
            cout<<"{ ";
            BFS(visited,i);
            cout<<"}"<<endl;
        }
    return 0;
}
